//
//  WebViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/8/17.
//  Copyright © 2018年 wjk. All rights reserved.
//

import UIKit
import WebKit

class WebViewController: UIViewController,WKUIDelegate, WKNavigationDelegate,UIWebViewDelegate {
    
    var index = 0
    var urlArr:[String] = [String]()
    var htmlStr = ""
    var webView:WKWebView?
    lazy private var progressView: UIProgressView = {
        self.progressView = UIProgressView.init(frame: CGRect(x: CGFloat(0), y: CGFloat(1), width: UIScreen.main.bounds.width, height: 2))
        self.progressView.tintColor = UIColor.green      // 进度条颜色
        self.progressView.trackTintColor = UIColor.white // 进度条背景色
        return self.progressView
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpWKwebView()
    }
    // 创建webview
    func setUpWKwebView() {
        
        
        
        let webview = WKWebView(frame: CGRect.init(x: 0, y: 0, width: deviceWidth(), height: deviceHight()-64))
        
        
        
        if htmlStr.count > 0 {
            
            webview.loadHTMLString(htmlStr, baseURL: (NSURL(string: AppConstants.apiEndpoint)! as URL))
        }else {
            
            let model = urlArr[index] as! String
            let url = NSURL(string: model)
            
            let request = NSURLRequest.init(url: url! as URL)
            webview.load(request as URLRequest)
        }
        self.view.addSubview(webview)
        self.view.addSubview(progressView)
        self.webView = webview
        self.webView?.uiDelegate = self
        self.webView?.navigationDelegate = self
//        self.title = model.name
        
        self.webView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
        self.webView?.addObserver(self, forKeyPath: "title", options: .new, context: nil)
        
        
    }
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        //  加载进度条
        if keyPath == "estimatedProgress"{
            progressView.alpha = 1.0
            progressView.setProgress(Float((self.webView?.estimatedProgress) ?? 0), animated: true)
            if (self.webView?.estimatedProgress ?? 0.0)  >= 1.0 {
                UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseOut, animations: {
                    self.progressView.alpha = 0
                }, completion: { (finish) in
                    self.progressView.setProgress(0.0, animated: false)
                })
            }
        }
        
        if keyPath == "title" {
            
                self.navigationItem.title = self.webView?.title
            
        }
    }
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!){
        
    }
    
    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!){
//        MBProgressHUD.hideAllHUDs(for: self.view, animated: true)
        self.navigationItem.title = webView.title
    }
    func webViewDidClose(_ webView: WKWebView) {
        
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
    }
    deinit {
        self.webView?.removeObserver(self, forKeyPath: "estimatedProgress")
        self.webView?.removeObserver(self, forKeyPath: "title")
        self.webView?.uiDelegate = nil
        self.webView?.navigationDelegate = nil
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    static public func showWebView(_ arr:[String],_ title:String) -> WebViewController {
        let web = WebViewController()
        web.urlArr = arr
        web.title = title
        return web
    }
    
}
